package com.java.multipledatasource.service;

import com.java.multipledatasource.dao.entity.FaceRecognition;
import com.java.multipledatasource.dao.entity.SysUser;
import com.java.multipledatasource.dao.mappers.mastermapper.FaceRecognitionMapper;
import com.java.multipledatasource.dao.mappers.slavemapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionSynchronization;
import org.springframework.transaction.support.TransactionSynchronizationManager;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author huxb
 * @date 2020/10/18 0018 下午 2:17
 * @Desciption
 */
@Component
public class TestService {
    @Resource
    private FaceRecognitionMapper faceRecognitionMapper;

    @Autowired
    private SysUserMapper sysUserMapper;



    @Transactional
    public void test() {
        List<FaceRecognition> recognitionList = faceRecognitionMapper.selectList(null);
        System.out.println(recognitionList.size());

        List<SysUser> sysUsers = sysUserMapper.selectList(null);
        System.out.println(sysUsers.size());

        SysUser sysUser = sysUsers.get(0);
        FaceRecognition faceRecognition = recognitionList.get(0);
        System.out.println( "face" + faceRecognition.getUserName());
        System.out.println( "sysUser" + sysUser.getUsername());
        faceRecognition.setUserName("sysUser");
        faceRecognitionMapper.updateById(faceRecognition);
    }

    @Transactional(rollbackFor = Exception.class)
    public void test1(){

        TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
            @Override
            public void suspend() {

            }
        });
        sysUserMapper.insert(new SysUser());
        this.test();
    }

    @Transactional(propagation = Propagation.NESTED, rollbackFor = Exception.class)
    public void test2(){
        sysUserMapper.insert(new SysUser());
    }
}
